#include "dnfs.h"
#include <stdio.h>
#include <stdlib.h>
#include <rpc/pmap_clnt.h>
#include <string.h>
#include <memory.h>
#include <sys/socket.h>
#include <netinet/in.h>

#ifndef SIG_PF
#define SIG_PF void(*)(int)
#endif

static void
co_2(struct svc_req *rqstp, register SVCXPRT *transp)
{
	union {
		struct co_write_in co_write_2_arg;
		struct co_read_in co_read_2_arg;
	} argument;
	union {
		int co_write_2_res;
		struct co_read_out co_read_2_res;
	} result;
	bool_t retval;
	xdrproc_t _xdr_argument, _xdr_result;
	bool_t (*local)(char *, void *, struct svc_req *);

	switch (rqstp->rq_proc) {
	case NULLPROC:
		(void) svc_sendreply (transp, (xdrproc_t) xdr_void, (char *)NULL);
		return;

	case co_write:
		_xdr_argument = (xdrproc_t) xdr_co_write_in;
		_xdr_result = (xdrproc_t) xdr_int;
		local = (bool_t (*) (char *, void *,  struct svc_req *))co_write_2_svc;
		break;

	case co_read:
		_xdr_argument = (xdrproc_t) xdr_co_read_in;
		_xdr_result = (xdrproc_t) xdr_co_read_out;
		local = (bool_t (*) (char *, void *,  struct svc_req *))co_read_2_svc;
		break;

	default:
		svcerr_noproc (transp);
		return;
	}
	memset ((char *)&argument, 0, sizeof (argument));
	if (!svc_getargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
		svcerr_decode (transp);
		return;
	}
	retval = (bool_t) (*local)((char *)&argument, (void *)&result, rqstp);
	if (retval > 0 && !svc_sendreply(transp, (xdrproc_t) _xdr_result, (char *)&result)) {
		svcerr_systemerr (transp);
	}
	if (!svc_freeargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
		fprintf (stderr, "%s", "unable to free arguments");
		exit (1);
	}
	if (!co_2_freeresult (transp, _xdr_result, (caddr_t) &result))
		fprintf (stderr, "%s", "unable to free results");

	return;
}

static void
so_2(struct svc_req *rqstp, register SVCXPRT *transp)
{
	union {
		struct so_unlink_in so_unlink_2_arg;
		struct so_truncate_in so_truncate_2_arg;
	} argument;
	union {
		int so_unlink_2_res;
		int so_truncate_2_res;
	} result;
	bool_t retval;
	xdrproc_t _xdr_argument, _xdr_result;
	bool_t (*local)(char *, void *, struct svc_req *);

	switch (rqstp->rq_proc) {
	case NULLPROC:
		(void) svc_sendreply (transp, (xdrproc_t) xdr_void, (char *)NULL);
		return;

	case so_unlink:
		_xdr_argument = (xdrproc_t) xdr_so_unlink_in;
		_xdr_result = (xdrproc_t) xdr_int;
		local = (bool_t (*) (char *, void *,  struct svc_req *))so_unlink_2_svc;
		break;

	case so_truncate:
		_xdr_argument = (xdrproc_t) xdr_so_truncate_in;
		_xdr_result = (xdrproc_t) xdr_int;
		local = (bool_t (*) (char *, void *,  struct svc_req *))so_truncate_2_svc;
		break;

	default:
		svcerr_noproc (transp);
		return;
	}
	memset ((char *)&argument, 0, sizeof (argument));
	if (!svc_getargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
		svcerr_decode (transp);
		return;
	}
	retval = (bool_t) (*local)((char *)&argument, (void *)&result, rqstp);
	if (retval > 0 && !svc_sendreply(transp, (xdrproc_t) _xdr_result, (char *)&result)) {
		svcerr_systemerr (transp);
	}
	if (!svc_freeargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
		fprintf (stderr, "%s", "unable to free arguments");
		exit (1);
	}
	if (!so_2_freeresult (transp, _xdr_result, (caddr_t) &result))
		fprintf (stderr, "%s", "unable to free results");

	return;
}



int
main (int argc, char **argv)
{
	register SVCXPRT *transp;

	if (osd_init() != 0) {
		printf("osd_init failed\n");
		return -1;
	}

	pmap_unset (CO, CO_VERS);
	pmap_unset (SO, SO_VERS);
	
	transp = svcudp_create(RPC_ANYSOCK);
	if (transp == NULL) {
		fprintf (stderr, "%s", "cannot create udp service.");
		exit(1);
	}
	if (!svc_register(transp, CO, CO_VERS, co_2, IPPROTO_UDP)) {
		fprintf (stderr, "%s", "unable to register (CO, CO_VERS, udp).");
		exit(1);
	}
	if (!svc_register(transp, SO, SO_VERS, so_2, IPPROTO_UDP)) {
		fprintf (stderr, "%s", "unable to register (SO, SO_VERS, udp).");
		exit(1);
	}
		
	transp = svctcp_create(RPC_ANYSOCK, 0, 0);
	if (transp == NULL) {
		fprintf (stderr, "%s", "cannot create tcp service.");
		exit(1);
	}
	if (!svc_register(transp, CO, CO_VERS, co_2, IPPROTO_TCP)) {
		fprintf (stderr, "%s", "unable to register (CO, CO_VERS, tcp).");
		exit(1);
	}
	if (!svc_register(transp, SO, SO_VERS, so_2, IPPROTO_TCP)) {
		fprintf (stderr, "%s", "unable to register (SO, SO_VERS, tcp).");
		exit(1);
	}
	svc_run ();
	fprintf (stderr, "%s", "svc_run returned");
	exit (1);
	/* NOTREACHED */
}
